package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjFactory;
import com.zb.entity.ZjFactoryRecharge;
import com.zb.extend.FactoryRechargeExtend;
import com.zb.mapper.ZjFactoryRechargeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjFactoryRechargeService;
import com.zb.service.ZjFactoryService;
import com.zb.vo.FactoryBalanceAdjustVo;
import com.zb.vo.FactoryRechargeCheckListSearchVo;
import com.zb.vo.FactoryRechargeListSearchVo;
import com.zb.vo.FactoryRechargeListVo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 厂商充值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-28
 */
@Service
public class ZjFactoryRechargeServiceImpl extends ServiceImpl<ZjFactoryRechargeMapper, ZjFactoryRecharge> implements ZjFactoryRechargeService {
    @Resource
    private ZjFactoryRechargeMapper factoryRechargeMapper;
    @Resource
    private ZjFactoryService factoryService;

    // 保存平台调整余额记录
    @Override
    public void save(AccounLoginInfo loginInfo, Long factoryId, FactoryBalanceAdjustVo adjustVo) {
        ZjFactoryRecharge recharge = new ZjFactoryRecharge();
        recharge.setId(IdWorker.getId());
        recharge.setFactoryId(factoryId);
        recharge.setCode("CZ" + DateFormatUtils.format(new Date(), "yyMMddHHmmss"));
        if (adjustVo.getAdjustMode() == 1) {
            recharge.setMoney(adjustVo.getAdjustMoney());
        } else {
            recharge.setMoney(new BigDecimal("0.0").subtract(adjustVo.getAdjustMoney()));
        }
        recharge.setStatus(2);
        recharge.setMode(5);
        recharge.setApplyInvoice(false);
        recharge.setCreateEmpType(1);
        recharge.setCreateEmp(loginInfo.getName());
        recharge.setPaymentTime(new Date());
        recharge.setRemark(adjustVo.getRemark());
        recharge.setCancelEmp("");
        recharge.setCancelRemark("");
        this.save(recharge);
    }

    // 获取商家详情之充值记录
    @Override
    public Map<String, Object> getFactoryRechargeList(Long factoryId, FactoryRechargeListSearchVo searchVo) {
        PageUtil.setPageLimit(searchVo);
        List<ZjFactoryRecharge> rechargeList = factoryRechargeMapper.getFactoryRechargeList(factoryId, searchVo);
        if (CollectionUtil.isEmpty(rechargeList)) {
            return PageUtil.pageList(searchVo.getPage(), searchVo.getLimit(), 0, new ArrayList<>());
        }
        QueryWrapper<ZjFactory> factoryQueryWrapper = new QueryWrapper<>();
        factoryQueryWrapper.select("id", "name")
                .in("id", rechargeList.stream().map(ZjFactoryRecharge::getFactoryId).distinct().toList());
        List<ZjFactory> factoryList = factoryService.list(factoryQueryWrapper);
        return PageUtil.pageList(searchVo.getPage(), searchVo.getLimit(), factoryRechargeMapper.getFactoryRechargeListCount(factoryId, searchVo),
                rechargeList.stream().map(recharge -> {
                    FactoryRechargeListVo vo = new FactoryRechargeListVo();
                    BeanUtil.copyProperties(recharge, vo);
                    vo.setCreateTime(recharge.getGmtCreate());
                    factoryList.stream().filter(f -> Objects.equals(f.getId(), recharge.getFactoryId())).findFirst().map(ZjFactory::getName).ifPresent(vo::setFactory);
                    return vo;
                }).toList());
    }

    // 获取商家充值审核表
    @Override
    public List<FactoryRechargeExtend> getFactoryRechargeCheckList(Long custAccId, FactoryRechargeCheckListSearchVo search) {
        return factoryRechargeMapper.getFactoryRechargeCheckList(custAccId, search);
    }

    // 获取商家充值审核列表总记录数
    @Override
    public long getFactoryRechargeCheckListCount(Long custAccId, FactoryRechargeCheckListSearchVo search) {
        return factoryRechargeMapper.getFactoryRechargeCheckListCount(custAccId, search);
    }
}
